home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BevelBorder.java < prev    next >
Text File  |  1998-06-30  |  7KB  |  224 lines

  1. /*
  2.  * @(#)BevelBorder.java    1.6 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20. package com.sun.java.swing.border;
  21.  
  22. import java.awt.Graphics;
  23. import java.awt.Insets;
  24. import java.awt.Rectangle;
  25. import java.awt.Color;
  26. import java.awt.Component;
  27.  
  28.  
  29. /**
  30.  * A class which implements a simple 2 line bevel border.
  31.  * <p>
  32.  * Warning: serialized objects of this class will not be compatible with
  33.  * future swing releases.  The current serialization support is appropriate
  34.  * for short term storage or RMI between Swing1.0 applications.  It will
  35.  * not be possible to load serialized Swing1.0 objects with future releases
  36.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  37.  * baseline for the serialized form of Swing objects.
  38.  *
  39.  * @version 1.6 02/02/98
  40.  * @author David Kloba
  41.  */
  42. public class BevelBorder extends AbstractBorder
  43. {
  44.     /** Raised bevel type. */
  45.     public static final int RAISED  = 0;
  46.     /** Lowered bevel type. */
  47.     public static final int LOWERED = 1;
  48.  
  49.     protected int bevelType;
  50.     protected Color highlightOuter;
  51.     protected Color highlightInner;
  52.     protected Color shadowInner;
  53.     protected Color shadowOuter;
  54.  
  55.     /**
  56.      * Creates a bevel border with the specified type and whose
  57.      * colors will be derived from the background color of the
  58.      * component passed into the paintBorder method.
  59.      * @param bevelType the type of bevel for the border
  60.      */
  61.     public BevelBorder(int bevelType) {
  62.         this.bevelType = bevelType;
  63.     }
  64.  
  65.     /**
  66.      * Creates a bevel border with the specified type, highlight and
  67.      * shadow colors.
  68.      * @param bevelType the type of bevel for the border
  69.      * @param highlight the color to use for the bevel highlight
  70.      * @param shadow the color to use for the bevel shadow
  71.      */
  72.     public BevelBorder(int bevelType, Color highlight, Color shadow) {
  73.         this(bevelType, highlight.darker(), highlight, shadow, shadow.brighter());
  74.     }
  75.  
  76.     /**
  77.      * Creates a bevel border with the specified type, highlight
  78.      * shadow colors.
  79.      * @param bevelType the type of bevel for the border
  80.      * @param highlightOuter the color to use for the bevel outer highlight
  81.      * @param highlightInner the color to use for the bevel inner highlight
  82.      * @param shadowOuter the color to use for the bevel outer shadow
  83.      * @param shadowInner the color to use for the bevel inner shadow
  84.      */
  85.     public BevelBorder(int bevelType, Color highlightOuter, Color highlightInner,
  86.                         Color shadowOuter, Color shadowInner) {
  87.         this(bevelType);
  88.         this.highlightOuter = highlightOuter;
  89.         this.highlightInner = highlightInner;
  90.         this.shadowOuter = shadowOuter;
  91.         this.shadowInner = shadowInner;
  92.     }
  93.  
  94.     /**
  95.      * Paints the border for the specified component with the specified
  96.      * position and size.
  97.      * @param c the component for which this border is being painted
  98.      * @param g the paint graphics
  99.      * @param x the x position of the painted border
  100.      * @param y the y position of the painted border
  101.      * @param width the width of the painted border
  102.      * @param height the height of the painted border
  103.      */
  104.     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  105.         if (bevelType == RAISED) {
  106.              paintRaisedBevel(c, g, x, y, width, height);
  107.  
  108.         } else if (bevelType == LOWERED) {
  109.              paintLoweredBevel(c, g, x, y, width, height);
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Returns the insets of the border.
  115.      * @param c the component for which this border insets value applies
  116.      */
  117.     public Insets getBorderInsets(Component c)       {
  118.     return new Insets(2, 2, 2, 2);
  119.     }
  120.  
  121.     /**
  122.      * Returns the outer highlight color of the bevel border.
  123.      */
  124.     public Color getHighlightOuterColor(Component c)   {
  125.         return highlightOuter != null? highlightOuter : 
  126.                                        c.getBackground().brighter().brighter();
  127.     }
  128.  
  129.     /**
  130.      * Returns the inner highlight color of the bevel border.
  131.      */
  132.     public Color getHighlightInnerColor(Component c)   {
  133.         return highlightInner != null? highlightInner :
  134.                                        c.getBackground().brighter();
  135.     }
  136.  
  137.     /**
  138.      * Returns the inner shadow color of the bevel border.
  139.      */
  140.     public Color getShadowInnerColor(Component c)      {
  141.         return shadowInner != null? shadowInner :
  142.                                     c.getBackground().darker();
  143.     }
  144.  
  145.     /**
  146.      * Returns the outer shadow color of the bevel border.
  147.      */
  148.     public Color getShadowOuterColor(Component c)      {
  149.         return shadowOuter != null? shadowOuter :
  150.                                     c.getBackground().darker().darker();
  151.     }
  152.  
  153.     /**
  154.      * Returns the type of the bevel border.
  155.      */
  156.     public int getBevelType()       {
  157.         return bevelType;
  158.     }
  159.  
  160.     /**
  161.      * Returns whether or not the border is opaque.
  162.      */
  163.     public boolean isBorderOpaque() { return true; }
  164.  
  165.     protected void paintRaisedBevel(Component c, Graphics g, int x, int y,
  166.                                     int width, int height)  {
  167.         Color oldColor = g.getColor();
  168.         int h = height;
  169.         int w = width;
  170.  
  171.         g.translate(x, y);
  172.  
  173.         g.setColor(getHighlightOuterColor(c));
  174.         g.drawLine(0, 0, 0, h-1);
  175.         g.drawLine(1, 0, w-1, 0);
  176.  
  177.         g.setColor(getHighlightInnerColor(c));
  178.         g.drawLine(1, 1, 1, h-2);
  179.         g.drawLine(2, 1, w-2, 1);
  180.  
  181.         g.setColor(getShadowOuterColor(c));
  182.         g.drawLine(1, h-1, w-1, h-1);
  183.         g.drawLine(w-1, 1, w-1, h-2);
  184.  
  185.         g.setColor(getShadowInnerColor(c));
  186.         g.drawLine(2, h-2, w-2, h-2);
  187.         g.drawLine(w-2, 2, w-2, h-3);
  188.  
  189.         g.translate(-x, -y);
  190.         g.setColor(oldColor);
  191.  
  192.     }
  193.  
  194.     protected void paintLoweredBevel(Component c, Graphics g, int x, int y,
  195.                                         int width, int height)  {
  196.         Color oldColor = g.getColor();
  197.         int h = height;
  198.         int w = width;
  199.  
  200.         g.translate(x, y);
  201.  
  202.         g.setColor(getShadowInnerColor(c));
  203.         g.drawLine(0, 0, 0, h-1);
  204.         g.drawLine(1, 0, w-1, 0);
  205.  
  206.         g.setColor(getShadowOuterColor(c));
  207.         g.drawLine(1, 1, 1, h-2);
  208.         g.drawLine(2, 1, w-2, 1);
  209.  
  210.         g.setColor(getHighlightOuterColor(c));
  211.         g.drawLine(1, h-1, w-1, h-1);
  212.         g.drawLine(w-1, 1, w-1, h-2);
  213.  
  214.         g.setColor(getHighlightInnerColor(c));
  215.         g.drawLine(2, h-2, w-2, h-2);
  216.         g.drawLine(w-2, 2, w-2, h-3);
  217.  
  218.         g.translate(-x, -y);
  219.         g.setColor(oldColor);
  220.  
  221.     }
  222.  
  223. }
  224.